package com.example.consumer_order_ribbon_hystrix_custom.Controller;

import com.example.consumer_order_ribbon_hystrix_custom.model.UserInf;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/order/user/getById")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "fallback")
    public Object getUserById(String id){
        System.out.println("--------------------"+id+"--------------");
        String url = "http://eureka-user/user/getById?id=" + id;
//        String url = "http://EUREKA_USER/user/getById?id=" + id;
//        String url = "http://localhost:8081/user/getById?id=" + id;
        return restTemplate.getForObject(url, Object.class);
    }

    public Object fallback(String id){
        UserInf userInf = new UserInf();
        userInf.setId(-1);
        userInf.setAge(100);
        userInf.setName("fallback");
        return  userInf;
    }

}
